var curURL=window.document.location.href;//如： http://csi.chinatet.com/manage/historyMsgListInit.shtm?roomID=74
var pathName=window.document.location.pathname;//如：/manage/historyMsgListInit.shtm
var pos=curURL.lastIndexOf(pathName);
var hostPath=curURL.substring(0,pos);//如：http://csi.chinatet.com
var _context=pathName.substring(0,pathName.substr(1).indexOf('/')+1);//如：/manage
var baseRoot = hostPath+_context; //如：http://csi.chinatet.com/manage
var _SSOServiceURLs = 'https://portal.cdeledu.com/auth';
function loginRegister(index){
	var contentStr = "";
	var height = "";
	var width = "";
	var type = 2;
	if(index == 2){
		height = "550";
		width = "450";
		//contentStr = "iframe://member.chinaacc.com/uc/member/register.shtm?type=" + type;
		contentStr = "iframe://" + document.domain + _context + "/login/register.shtm?type=" + type;
	}else{
		height = "550";
		width = "450";
		contentStr = "iframe://" + document.domain + _context + "/login/login.shtm?type=" + type;
	}
	dialogShow(contentStr,width,height,"from","210");
}
var dialogFirst=true;
function dialogShow(content,width,height,cssName,topSpace,showbg,offsets,obj){
	if(dialogFirst==true){
		var temp_float=new String;
		temp_float="<div id=\"floatBoxBg\" style=\"height:"+$(document).height()
			+"px;filter:alpha(opacity=0);opacity:0;\"></div>";
		temp_float+="<div id=\"floatBox\" >";
		temp_float+="<div class=\"contents\"></div>";
		temp_float+="</div>";
		$("body").append(temp_float);
		dialogFirst=false;
	}
	contentType=content.substring(0,content.indexOf(":"));
	content=content.substring(content.indexOf(":")+1,content.length);
	var cStr="<iframe id='frame4login' src=\""+content+"\" width=\"450px\" height=\""
		+(parseInt(height))+"px"
		+"\" scrolling=\"no\" allowtransparency=\"true\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>";
	$("#floatBox .contents").html(cStr);

	if(navigator.userAgent.indexOf("TencentTraveler")>0){
		$("#frame4login").attr("src",content);
	}
	if(showbg != false){
		$("#floatBoxBg").show();
		$("#floatBoxBg").animate({opacity:"0.3"},"normal");
	}
	$("#floatBox").attr("class","floatBox "+cssName);
	if(offsets == null && obj == null){
		$("#floatBox").css({
			display:"block",
			left:(($(document).width())/2-(parseInt(width)/2))+"px",
			top:($(document).scrollTop()-(height=="auto"?300:parseInt(height)))+"px",
			width:(parseInt(width)+2)+"px",
			height:height,border:"none",
			background:"none"});
		$("#floatBox").animate({
			top:($(document).scrollTop()+$(window).height()/2-parseInt(topSpace))+"px"},"normal");
	}
}
function closeWin(){
	$("#floatBoxBg").remove();
	$("#floatBox").remove();
	$("select").attr("disabled","");
	dialogFirst=true;
}
function switchWindow(index){
	closeWin();
	loginRegister(index);
}
function lmRegister(uid) {
	$.ajax({
		type : "get",
		async : false,
		url : "http://union.chinaacc.com/union/register/register.shtm?uid="+uid,
		dataType : "jsonp",
		jsonp : "callback",
		jsonpCallback : "flightHandler",
		success : function(data) {},
		error : function() {}
	});
}
function onloadW(){
	window.location.reload();
}


function getCookie(name){
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
     if(arr=document.cookie.match(reg))
         return unescape(arr[2]);
    else
        return null;
}

//检测portal登录状态并自动登录
function portalVerify(_loginConfig){
	var popKey = $("#pop_key").val();
	if(!_loginConfig 
			|| !_loginConfig.hasOwnProperty("turl") 
			|| !_loginConfig.hasOwnProperty("furl") 
			|| !_loginConfig.hasOwnProperty("eurl")
			|| _isBlank(_loginConfig["turl"])
			|| _isBlank(_loginConfig["furl"])
			|| _isBlank(_loginConfig["eurl"])
		){
		return;
	}
	var _SSOServiceURLs = _loginConfig._SSOServiceURLs;
	if(_isBlank(_SSOServiceURLs)){
		_SSOServiceURLs = 'https://portal.cdeledu.com/auth';
	}
	$.getJSON(_SSOServiceURLs + "/index.php?cmd=ucCheckLogin&type=JSON&jsonpCallback=?",
		function(json){
			var code = json.code;
			if(code == "0") {
				var validateLogin = getUrlParameterAdv("validateLogin",location.href);
				if(validateLogin != "n") {
					var config = {} ;
					config['action'] = _SSOServiceURLs + "/index.php" ;
					config['fields'] =["cmd", "type", "turl", "furl", "eurl"];
					config['values'] = ["ucCheckLogin", "JS", _loginConfig.turl, _loginConfig.furl, _loginConfig.eurl];
					config['target'] = "_self";
					var form = newForm(config);
					if(null!=form){
						form.submit();
					}
				}
			}else{
				var sid = getCookie("sid");
				var url = baseRoot + "/login/loginDispose.shtm";
				var param = {
					'type' : 'json',
					'sid' : sid
				}
				$.post(url,param,function(data){
					var res = eval("(" + $.trim(data) + ")");
					if(res.code == '200'){
						window.location.href = _loginConfig.furl;
					}else{
						//自动登录失败，弹出登录框
						if(popKey != 1){
							loginRegister(1);
						}
					}
				});
			}
	});
}
function logout(){
	var url = hostPath + _context;
	url = url.replace('member','www');
	url = encodeURIComponent(url);//防止queryString被删除
	$("body").append('<iframe src="http://member.chinaacc.com/member/logout.shtm" style="display:none;width:0;height:0;"></iframe>');//从会计网退出
	$("body").append('<iframe src="'+_SSOServiceURLs+'/index.php?cmd=ucLogout" style="display:none;width:0;height:0;"></iframe>');//从会计网退出
	$.post(baseRoot + "/login/logout.shtm",{
		"type":'json'
	},function(data){
		var res = $.trim(data);
		res = eval("("+res+")");
		if(res.code==200){
			if(baseRoot.indexOf("www")>0){
				baseRoot = baseRoot.replace("www","member");
			}else{
				baseRoot = baseRoot.replace("member","www");
			}
			window.location.href = baseRoot + "/login/logout.shtm?gotoURL="+url;//从开放课堂退出
		}
	});
}
function newForm(_config){
	var action = _config.action;
	var fields = _config.fields;
	var values = _config.values;
	var target = _config.target;
	if(target==null||target==''||target==undefined){
		target = "_blank";
	}
	var form = document.createElement("form");
	form.action = action;
	form.target = target;
	form.method = "post";
	parent.document.body.appendChild(form);
	var i;
	if(fields.length==values.length){
		for(i=0;i<fields.length;i++){
			var input = document.createElement("input");
			input.type = "hidden";
			input.name = fields[i];
			input.value = values[i];
			form.appendChild(input);
		}
		return form;
	}else{
		return null;
	}
}
//取js参数
//para   asName   String:要得到的参数名 
//para   lsURL   String:要处理的URL值 
//这个函数是在html传参数函数基础上改的 
function getUrlParameterAdv(asName,lsURL){ 
	/*var loU = lsURL.split( "?");
	if(loU.length> 1){
		var loallPm = loU[1].split("&");
		for(var i=0; i < loallPm.length; i++){
			var loPm = loallPm[i].split( "=");
			if(loPm[0]==asName){
				if(loPm.length> 1){
					return loPm[1];
				}else{
					return "";
				}
			}
		}
	}
	return null;*/
	if(lsURL.indexOf("?") != -1) {
 		var loU = lsURL.substr(lsURL.indexOf("?")+1);
		var loallPm = loU.split("&");
		for(var i=0; i < loallPm.length; i++){
			var loPm = loallPm[i].split( "=");
			if(loPm[0]==asName){
				if(loPm.length> 1){
					return loPm[1] ;
				}else{
					return "";
				}
			}
		}
	}else{
		return "";
	}
}
function addParam2URL(_url,params){
	var _urlStr = _url;
	var paramStr = '';
	if(_url.indexOf('?') != -1){
		_urlStr = _url.substring(0, _url.indexOf('?'));
		paramStr = _url.substring(_url.indexOf('?') + 1, _url.length);
	}
	var localParams = {};
	var localParamArr = paramStr.split("&");
	for(var i in localParamArr){
		if(localParamArr[i].indexOf('=') != -1){
			var paramArr = localParamArr[i].split('=');
			localParams[paramArr[0]] = paramArr[1];
		}
	}
	for(var k in params){
		localParams[k] = params[k];
	}
	var i = 0;
	for(var k in localParams){
		if(i == 0){
			_urlStr += '?';
		}else{
			_urlStr += '&';
		}
		i++;
		_urlStr = _urlStr + k + '=' + localParams[k];
	}
	return _urlStr;
}
//检测字符串是否为空
function _isBlank(value){
	if (value == null || value == 'undefined') {
		return true;
	}
	var str = $.trim(value);
	return str.length == 0;
}